﻿using MyTodo.Services.Interfaces;
using MyTodo.Shared.Dtos;
using MyTodo.Shared.Parameters;
using Prism.Commands;
using Prism.Mvvm;
using System.Collections.ObjectModel;

namespace MyTodo.ViewModels;

public class ToDoViewModel : BindableBase
{
    private readonly IToDoService _service;
    private bool _isRightDrawerOpen;

    public ToDoViewModel(IToDoService service)
    {
        _service = service;
        AddCommand = new DelegateCommand(AddToDo);
        CreateTestData();
    }

    public ObservableCollection<ToDoDto> ToDoDtos { set; get; } = new ObservableCollection<ToDoDto>();

    public DelegateCommand AddCommand { get; private set; }

    public bool IsRightDrawerOpen
    {
        get => _isRightDrawerOpen;
        set
        {
            _isRightDrawerOpen = value; RaisePropertyChanged();
        }
    }

    private void AddToDo()
    {
        IsRightDrawerOpen = true;
    }

    private async void CreateTestData()
    {
        var todoResult = await _service.GetAllAsync(new QueryParameter() { PageIndex = 0, PageSize = 100});
        if (todoResult.Status && todoResult.Result != null)
        {
            ToDoDtos.Clear();
            ToDoDtos.AddRange(todoResult.Result.Items);
        }
    }

}
